package com.example.pan.ui.download

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.example.pan.R
import com.example.pan.databinding.ItemTaskInfoBinding
import com.example.pan.model.DownloadTaskInfo
import com.example.pan.ui.base.BaseViewHolder
import java.util.*

class DownloadAdapter :
    ListAdapter<DownloadTaskInfo, BaseViewHolder<ItemTaskInfoBinding>>(Callback()) {

    private var listener: OnItemClickListener? = null

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseViewHolder<ItemTaskInfoBinding> {
        return BaseViewHolder(
            ItemTaskInfoBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: BaseViewHolder<ItemTaskInfoBinding>, position: Int) {
        val item = getItem(position)
        with(holder.binding) {
            item?.let {
                fileName.text = it.fileName
                when (it.status) {
                    "active" -> {
                        pause.setImageResource(R.drawable.ic_pause_black_24dp)
                        pause.setOnClickListener { _ ->
                            listener?.onPause(it)
                        }
                        speed.text = it.downloadSpeed
                    }
                    "waiting" -> {
                        pause.setImageResource(R.drawable.ic_play_arrow_black_24dp)
                        pause.setOnClickListener { _ ->
                            listener?.onUnPause(it)
                        }
                        speed.text = "等待下载"
                    }
                    "paused" -> {
                        pause.setImageResource(R.drawable.ic_play_arrow_black_24dp)
                        pause.setOnClickListener { _ ->
                            listener?.onUnPause(it)
                        }
                        speed.text = "暂停下载"
                    }
                    "error" -> {
                        speed.text = it.errorMsg
                    }
                    "complete" -> {
                        speed.text = "下载完成"
                    }
                    "removed" -> {
                        speed.text = "已移除"
                    }
                }
                progressBar.progress = it.process
                simpleInfo.text = "${it.completeLength}/${it.totalLength}"
                remove.setOnClickListener { _ ->
                    listener?.onRemove(it)
                }
            }

        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: BaseViewHolder<ItemTaskInfoBinding>,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position)
        } else {
            val item = getItem(position)
            with(holder.binding) {
                item?.let {
                    when (it.status) {
                        "active" -> {
                            pause.setImageResource(R.drawable.ic_pause_black_24dp)
                            pause.setOnClickListener { _ ->
                                listener?.onPause(it)
                            }
                            speed.text = it.downloadSpeed
                        }
                        "waiting" -> {
                            pause.setImageResource(R.drawable.ic_play_arrow_black_24dp)
                            pause.setOnClickListener { _ ->
                                listener?.onUnPause(it)
                            }
                            speed.text = "等待下载"
                        }
                        "paused" -> {
                            pause.setImageResource(R.drawable.ic_play_arrow_black_24dp)
                            pause.setOnClickListener { _ ->
                                listener?.onUnPause(it)
                            }
                            speed.text = "暂停下载"
                        }
                        "error" -> {
                            speed.text = it.errorMsg
                        }
                        "complete" -> {
                            speed.text = "下载完成"
                        }
                        "removed" -> {
                            speed.text = "已移除"
                        }
                    }
                    progressBar.progress = it.process
                    simpleInfo.text = "${it.completeLength}/${it.totalLength}"
                    remove.setOnClickListener { _ ->
                        listener?.onRemove(it)
                    }
                }
            }
        }
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.listener = listener
    }

    interface OnItemClickListener {
        fun onItemClick(item: DownloadTaskInfo)

        fun onPause(item: DownloadTaskInfo)

        fun onUnPause(item: DownloadTaskInfo)

        fun onRemove(item: DownloadTaskInfo)
    }

    internal class Callback : DiffUtil.ItemCallback<DownloadTaskInfo>() {
        override fun areItemsTheSame(
            oldItem: DownloadTaskInfo,
            newItem: DownloadTaskInfo
        ): Boolean {
            return TextUtils.equals(oldItem.gid, newItem.gid)
        }

        override fun areContentsTheSame(
            oldItem: DownloadTaskInfo,
            newItem: DownloadTaskInfo
        ): Boolean {
            return Objects.equals(oldItem, newItem)
        }

        override fun getChangePayload(oldItem: DownloadTaskInfo, newItem: DownloadTaskInfo): Any {
            return newItem
        }
    }

}